using FastDevTemplate.Common.Util;
using FastDevTemplate.DataAccess;
using FastDevTemplate.DataAccess.Entities;
using FastDevTemplate.Service.Base;
using FastDevTemplate.Service.Interface;
using FastDevTemplate.Service.Interface.DTO;

namespace FastDevTemplate.Service;

public class LogService(FastDevTemplateDbContext dbContext) : 
    BaseQueryService<Log,Guid,LogResultDto,LogResultDto>(dbContext), 
    ILogService
{
    
    public async Task<int> AddAsync(LogAddDto dto)
    {
        var entity = ObjectMapper.Map<Log>(dto);
        DbSet.Add(entity);
        try
        {
            var count = await Dao.SaveChangesAsync();
            if (count <= 0)
                return ErrorHandling(ErrorType.DataBaseSaveError, nameof(AddAsync));
            return count;
        }
        catch (Exception e)
        {
            return ErrorHandling(ErrorType.DataBaseSaveError, e);
        }
    }
}